package com.yantong.controller;

import com.yantong.pojo.Category;
import com.yantong.service.CategoryService;
import com.yantong.pojo.ProductGrade;
import com.yantong.service.ProductGradeService;
import com.yantong.annotation.Log;
import com.yantong.constant.CategoryType;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.ListMemberParams;
import com.yantong.pojo.Member;
import com.yantong.service.CommanderListService;
import com.yantong.utils.ValidateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品分类
 *
 * @author dcx
 * @date 2018年8月28日
 */
@Controller
@RequestMapping("/category/manage")
public class CategoryController extends BaseController {
	private static final String prefix = "category";

	@Autowired
	private CategoryService categoryService;
	@Autowired
	private CommanderListService commanderListService;
	@Autowired
	private ProductGradeService productGradeService;

	/**
	 * 跳转分类页面
	 *
	 * @return
	 */
	@GetMapping()
	@RequiresPermissions("category:manage:manage")
	public String categorys(Model model) {
		model.addAllAttributes(getCentreIdModelInfo());
		return prefix + "/categorys";
	}

	/**
	 * 列表
	 *
	 * @return
	 * @author dcx
	 * @date 2018年8月28日-上午11:41:11
	 */
	@GetMapping("/list")
	@ResponseBody
	@RequiresPermissions("category:manage:manage")
	public List<Category> listCategorys(Long parentid, Long centreId) {
		Category category = new Category();
		category.setCentreId(centreId);
		category.setLiansuoId(getUserTeamId());
		return categoryService.listCategorys(category);

	}

	/**
	 * 跳转新增页面
	 *
	 * @return
	 * @author dcx
	 * @date 2018年8月30日-下午8:21:37
	 */
	@GetMapping("/toAdd")
	@RequiresPermissions("category:manage:add")
	public String toAdd(Model model) {
		if (getUserCentreId() != null) {
			model.addAttribute("baTypes", categoryService.listCategorys(0L, null, getUserCentreId(),getUserTeamId()));
		} else {
			model.addAttribute("baTypes", categoryService.listCategorys(0L));
		}
		model.addAllAttributes(getCentreIdModelInfo());
		model.addAttribute("teamId",getUserTeamId());
		ListMemberParams listMemberParams = new ListMemberParams();
		if (getUserCentreId() != null) {
			listMemberParams.setCentreId(getUserCentreId());
		}
		List<Member> teams = commanderListService.listCommanders(listMemberParams, new Member());
		List<ProductGrade> productGradeList = productGradeService.selectByAll();
		model.addAttribute("productGrades", productGradeList);
		model.addAttribute("teams", teams);
		return prefix + "/add";
	}

	/**
	 * 新增
	 *
	 * @param category
	 * @return
	 * @author dcx
	 * @date 2018年8月28日-上午11:41:19
	 */
	@Log(module = "商品分类管理", operation = "添加分类")
	@RequiresPermissions("category:manage:add")
	@PostMapping("/save")
	@ResponseBody
	public ResultVo save(Category category) {
		if (category == null) {
			return ResultVo.error();
		}
		if(category.getLiansuoId() == null){
			category.setLiansuoId(getUserTeamId());
		}

		if (categoryService.save(category) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	/**
	 * 跳转到编辑页面
	 *
	 * @param centreId
	 * @param model
	 * @return
	 * @author dcx
	 * @date 2018年8月28日-下午2:12:57
	 */
	@RequiresPermissions("category:manage:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable Long id, Model model) {
		model.addAttribute("obj", categoryService.getByKey(id));
		model.addAttribute("baTypes", categoryService.listCategorys(0L));

		ListMemberParams listMemberParams = new ListMemberParams();
		if (getUserCentreId() != null) {
			listMemberParams.setCentreId(getUserCentreId());
		}
		List<Member> teams = commanderListService.listCommanders(listMemberParams, new Member());
		List<ProductGrade> productGradeList = productGradeService.selectByAll();
		model.addAttribute("productGrades", productGradeList);
		model.addAttribute("teams", teams);
		return prefix + "/edit";
	}

	/**
	 * 更新分类
	 *
	 * @param obj
	 * @return
	 * @author dcx
	 * @date 2018年8月28日-下午2:13:49
	 */
	@Log(module = "商品分类管理", operation = "编辑分类")
	@RequiresPermissions("category:manage:edit")
	@PostMapping("/update")
	@ResponseBody
	public ResultVo update(Category obj) {
		if (obj == null || obj.getId() == null) {
			return ResultVo.error();
		}

		if (categoryService.update(obj) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	/**
	 * 删除分类
	 *
	 * @param id
	 * @return
	 * @author dcx
	 * @date 2018年8月28日-下午2:20:00
	 */
	@Log(module = "商品分类管理", operation = "删除分类")
	@RequiresPermissions("category:manage:remove")
	@PostMapping("/remove")
	@ResponseBody
	public ResultVo remove(Long id) {
		if (id == null) {
			return ResultVo.error();
		}
		if (categoryService.remove(id) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	/**
	 * 复制分类
	 *
	 * @param idmanage
	 * @return
	 * @author dcx
	 * @date 2018年8月28日-下午2:20:00
	 */
	@Log(module = "商品分类管理", operation = "复制分类")
	@RequiresPermissions("category:manage:copy")
	@PostMapping("/copy")
	@ResponseBody
	public ResultVo copy(Long id) {
		if (id == null) {
			return ResultVo.error();
		}
		if (categoryService.copy(id) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	/**
	 * 批量复制
	 *
	 * @param ids
	 * @return
	 * @author baolingyu
	 * @date 2018年8月28日-下午2:20:00
	 */
	@RequiresPermissions("product:centre:copy")
	@PostMapping("/copyBatch")
	@ResponseBody
	public ResultVo copyBatch(@RequestParam("ids[]") Long[] ids) {
		if (ids == null || ids.length <= 0) {
			return ResultVo.error();
		}
		for (Long id : ids) {
			categoryService.copy(id);
		}
		return ResultVo.ok();
	}

	/**
	 * 根据父类id获取子类列表
	 *
	 * @param id
	 * @return
	 * @author dcx
	 * @date 2018年9月3日-下午4:40:44
	 */
	@RequiresPermissions("category:manage:getChildByPar")
	@GetMapping("/getChildByPar")
	@ResponseBody
	public List<Category> getChildByPar(Long id) {
		if (id == null) {
			return new ArrayList<>();
		}
		List<Category> list = categoryService.listCategorys(id);
		for (Category category : list) {
			if (category.getType() == CategoryType.houtaileimu.getType()) {
				category.setTypeName("活动类目");
			} else if (category.getType() == CategoryType.qiantaileimu.getType()) {
				category.setTypeName("前台类目");
			} else if (category.getType() == CategoryType.houtaileimu.getType()) {
				category.setTypeName("后台类目");
			} else if (category.getType() == CategoryType.zhuyeleimu.getType()) {
				category.setTypeName("主页类目");
			} else if (category.getType() == CategoryType.waimaileimu.getType()) {
				category.setTypeName("外卖类目");
			}
//			else if(category.getType()==CategoryType.tangshileimu.getType()){
//				category.setTypeName("堂食类目");
//			}else if(category.getType()==CategoryType.zitileimu.getType()){
//				category.setTypeName("自提类目");
//			}
		}
		return list;
	}


	/**
	 * 获取下级类目列表
	 *
	 * @param parentId
	 * @return
	 */
	@ResponseBody
	@PostMapping("/ccates")
	public ResultVo getCcateList(Long parentId) {
		if (parentId == null) {
			return ResultVo.error();
		}
		return ResultVo.ok().put("ccates", categoryService.listCategorys(parentId));
	}

	/**
	 * 获取分类信息详情
	 *
	 * @param categoryId
	 * @return
	 */
	@ResponseBody
	@PostMapping("/getCategoryDetail")
	public ResultVo getCategoryDetail(Long categoryId) {
		if (categoryId == null) {
			return ResultVo.error();
		}
		return ResultVo.ok().put("category", categoryService.getByKey(categoryId));
	}

	@GetMapping("/getCategorysByType")
	@ResponseBody
	public List<Category> getCategorysByType(Integer type) {
		if (type == null) {
			return new ArrayList<>();
		}
		return categoryService.listCategorys(0L, type, getUserCentreId(),getUserTeamId());
	}

	/**
	 * 跳转页面：选择分类页面
	 */
	@GetMapping("/toCategory")
	public String toCategoryOne(Model model, String idDom, String nameDom, Integer singleSelect, String callback, Long parentid, int count) {
		model.addAttribute("idDom", idDom);
		model.addAttribute("nameDom", nameDom);
		model.addAttribute("singleSelect", singleSelect);
		model.addAttribute("callback", callback);
		model.addAttribute("parentid", parentid);
		model.addAttribute("count", count);
		return "product/centre/toCategory";
	}

	/**
	 * 跳转页面
	 *
	 * @return
	 */
	@GetMapping("/categorys")
	@ResponseBody
	public PageUtils<Category> products(Category category, int offset, int limit) {
		Map<String, Object> map = getCentreIdModelInfo();
		//商户
		if (StringUtils.isNotEmpty(map.get("flag").toString()) && (map.get("flag").equals("1") || map.get("flag").equals("6"))) {
			if (category.getParentid() == null) {
				category.setParentid(0L);
			}
			category.setCentreId(new Long(map.get("centreId").toString()));
		} else if (StringUtils.isNotEmpty(map.get("flag").toString()) && map.get("flag").equals("0")) {
			if (category.getParentid() == null) {
				category.setParentid(0L);
			}
		}
//		if(getZbFlag()){
//			if(ValidateUtil.isNotEmptyObjectOrString(getUserTeamId())){
//				category.setLiansuoId(getUserTeamId());
//			}
//		}
		return categoryService.queryCategoryPage(category, offset, limit);
	}
}
